

import ws.*;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.event.ActionEvent;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

public class ProfileBean implements Serializable{

    private List<StatusUI> updates;
    private List<UserUI> friends;
    
    public ProfileBean() {
    }

    public List<StatusUI> getUpdates() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UserUI user = (UserUI)session.getAttribute("User");
        ws.RoMaWS_Service service = new ws.RoMaWS_Service();
        ws.RoMaWS port = service.getRoMaWSPort();
        //JAX-WX Auth.
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList(loginBean.getName()));
        headers.put("Password", Collections.singletonList(loginBean.getPassword()));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        if(user != null){
            updates = port.getFriendStatusUpdates(user.getId());
        }
        return updates;
    }

    public void setUpdates(List<StatusUI> updates) {
        this.updates = updates;
    }

    public List<UserUI> getFriends() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UserUI user = (UserUI)session.getAttribute("User");
        ws.RoMaWS_Service service = new ws.RoMaWS_Service();
        ws.RoMaWS port = service.getRoMaWSPort();
        //JAX-WX Auth.
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList(loginBean.getName()));
        headers.put("Password", Collections.singletonList(loginBean.getPassword()));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        if(user != null){
            friends = port.getUserFriends(user.getId());
        }
        return friends;
    }

    public void setFriends(List<UserUI> friends) {
        this.friends = friends;
    }
    
    public void removeFriendListener(ActionEvent event){
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        UserUI user = (UserUI)session.getAttribute("User");
        ws.RoMaWS_Service service = new ws.RoMaWS_Service();
        ws.RoMaWS port = service.getRoMaWSPort();
        //JAX-WX Auth.
        FacesContext context = FacesContext.getCurrentInstance();
        LoginBean loginBean = (LoginBean) context.getApplication().evaluateExpressionGet(context, "#{loginBean}", LoginBean.class);
        Map<String, Object> req_ctx = ((BindingProvider)port).getRequestContext();
        req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WSUrl.wsUrl);
        Map<String, List<String>> headers = new HashMap<String, List<String>>();
        headers.put("Username", Collections.singletonList(loginBean.getName()));
        headers.put("Password", Collections.singletonList(loginBean.getPassword()));
        req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);
        port.removeFriendship(user.getId(),((UserUI) event.getComponent().getAttributes().get("selection")).getId());

    }

}
